'use client'

import {useState} from 'react'
import type {ProColumns} from '@ant-design/pro-components'
import {ProTable} from '@ant-design/pro-components'
import {Button, message, Modal, Tag} from 'antd'
import {PlusOutlined} from '@ant-design/icons'

type RoleItem = {
  id: string
  name: string
  code: string
  description: string
  status: 'active' | 'inactive'
  permissions: string[]
  createTime: string
  updateTime: string
}

export default function RolesPage() {
  const [createModalVisible, setCreateModalVisible] = useState(false)

  const columns: ProColumns<RoleItem>[] = [
    {
      title: '角色名称',
      dataIndex: 'name',
      width: 120,
    },
    {
      title: '角色编码',
      dataIndex: 'code',
      width: 120,
    },
    {
      title: '描述',
      dataIndex: 'description',
      width: 200,
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: 100,
      valueEnum: {
        active: { text: '启用', status: 'Success' },
        inactive: { text: '禁用', status: 'Error' },
      },
    },
    {
      title: '权限',
      dataIndex: 'permissions',
      width: 300,
      render: (_, record) => (
        <div style={{ display: 'flex', gap: '4px', flexWrap: 'wrap' }}>
          {record.permissions.map((perm) => (
            <Tag key={perm}>{perm}</Tag>
          ))}
        </div>
      ),
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 180,
      valueType: 'dateTime',
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      width: 180,
      valueType: 'dateTime',
    },
    {
      title: '操作',
      width: 180,
      key: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="edit"
          onClick={() => {
            message.info('编辑功能开发中')
          }}
        >
          编辑
        </a>,
        <a
          key="permission"
          onClick={() => {
            message.info('权限配置功能开发中')
          }}
        >
          权限配置
        </a>,
        <a
          key="delete"
          onClick={() => {
            Modal.confirm({
              title: '确认删除',
              content: '确定要删除这个角色吗？',
              onOk: () => {
                message.success('删除成功')
              },
            })
          }}
        >
          删除
        </a>,
      ],
    },
  ]

  const mockData: RoleItem[] = [
    {
      id: '1',
      name: '超级管理员',
      code: 'SUPER_ADMIN',
      description: '系统最高权限角色',
      status: 'active',
      permissions: ['系统管理', '用户管理', '角色管理', '权限管理', '菜单管理'],
      createTime: '2024-03-01 10:00:00',
      updateTime: '2024-03-01 10:00:00',
    },
    {
      id: '2',
      name: '普通用户',
      code: 'NORMAL_USER',
      description: '普通用户角色',
      status: 'active',
      permissions: ['商品查看', '订单管理', '个人信息管理'],
      createTime: '2024-03-01 11:00:00',
      updateTime: '2024-03-01 11:00:00',
    },
  ]

  return (
    <ProTable<RoleItem>
      columns={columns}
      dataSource={mockData}
      rowKey="id"
      search={{
        labelWidth: 120,
      }}
      toolBarRender={() => [
        <Button
          key="button"
          icon={<PlusOutlined />}
          type="primary"
          onClick={() => setCreateModalVisible(true)}
        >
          新建
        </Button>,
      ]}
    />
  )
} 